import { _decorator, Component, director, Label, Node, Sprite, SpriteFrame } from 'cc';
import { loadRes } from '../utils/loadRes';
import { gameConfig } from '../data/gameConfig';
import { nodePool } from '../utils/nodePool';
import { emits } from '../data/enums';
const { ccclass, property } = _decorator;

@ccclass('levelItem')
export class levelItem extends Component {
    private levelNum: number = 0;
    type: number = 0;
    @property(Sprite)
    levelSprite: Sprite = null;
    @property(Node)
    levelNumLabel: Node = null;
    @property(Sprite)
    star: Sprite = null;
    start() {

    }

    init() {
        let tmp = (gameConfig.currChapter - 1) * 16;
        let levels = gameConfig.levelList;
        this.levelNum += tmp;
        this.type = levels[this.levelNum];
        switch (this.type) {
            case -1:
            case null:
                this.levelNumLabel.active = false;
                this.levelSprite.spriteFrame = loadRes.ins.getSpriteFrame("Locked");
                break;
            case 0:
                this.levelNumLabel.getComponent(Label).string = this.levelNum.toString();
                this.levelSprite.spriteFrame = loadRes.ins.getSpriteFrame("Unlocked");
                this.star.spriteFrame = loadRes.ins.getSpriteFrame("star0");
                break;
            case 1:
            case 2:
            case 3:
                this.levelNumLabel.getComponent(Label).string = this.levelNum.toString();
                this.levelSprite.spriteFrame = loadRes.ins.getSpriteFrame("Dummy");
                this.star.spriteFrame = loadRes.ins.getSpriteFrame(`star${this.type}`);
                break;
        }
    }
    click() {
        if (this.type > -1) {
            if (gameConfig.levelList.length-1 < this.levelNum) {
                nodePool.ins.getPoolNode("toast", this.node.parent.parent);
                director.emit(emits.toast, "暂未开放");
                return;
            }
            gameConfig.currentLevelNum = this.levelNum;
            director.loadScene('game');
        }
    }
}


